package protocol

import (
	"context"
	"fmt"
	"net/http"

	"gitee.com/liuxiaowei123/vblog-1.1/config"
	"gitee.com/liuxiaowei123/vblog-1.1/ioc"
	"github.com/gin-gonic/gin"
)

type HttpServer struct {
	svc *http.Server
}

func NewHttpServer() *HttpServer {
	r := gin.Default()
	ioc.IocApiHandler().RouteRegistry(r.Group("/api/vblog"))
	return &HttpServer{
		svc: &http.Server{
			Addr:    fmt.Sprintf("%s:%d", config.C.Api.Apiadress, config.C.Api.Apiport),
			Handler: r,
		},
	}
}

func (h *HttpServer) Run() {
	h.svc.ListenAndServe()
}

func (h *HttpServer) Close(c context.Context) {
	h.svc.Shutdown(c)
}
